<?php
namespace App\Admin\Services;

use App\Admin\Models\AdminPaytypeList;
use App\Admin\Models\Financial\FinancialMoneylog;
use App\Admin\Models\Financial\FinancialRate;


class Financial
{
    public static function getFeeByMoney($money,$pay_type_id)
    {
        $rateinfo = FinancialRate::where([['money_start','<=',$money],['money_end','>=',$money],['pay_type_id',$pay_type_id],['is_del',1]])->first();
        if($rateinfo){
            if($rateinfo->val_type==1){
                return $rateinfo->regular_value;
            }else{
                return ceil(bcmul($money,$rateinfo->rate_value,2)/100);
            }
        }else{
            return 0;
        }
    }

    public static function getmoneylogcount($type,$receipt_type){
        $log_date_s=date('Y-m').'-01';
        $count=FinancialMoneylog::where([['type',$type],['receipt_type',$receipt_type],['log_date','>=',$log_date_s]])->count();
        $count+=1;
        return ['count'=>$count,'no'=>date('n').'-'.$count.'#'];
    }

    public static function createMoneyLog($pay_type_id,$income_money,$sn_code){
        $data=['pay_type_id'=>$pay_type_id,'income_money'=>$income_money,'expenditure_money'=>0,'log_date'=>date('Y-m-d'),'subject_id'=>-1,'create_at'=>time(),'update_at'=>time()];
        $pay_type_info=AdminPaytypeList::where(['id'=>$pay_type_id])->first();
        $pay_type_name=$pay_type_info?$pay_type_info->pay_type_name:'';
        $pay_type_type=$pay_type_info?$pay_type_info->type:1;

        if (strpos($pay_type_name, '现金') !== false || $pay_type_type==3){
            $data['type']=1;
            $data['receipt_type']=1;
            $receipt_type_name='现收';
        }else{
            $data['type']=2;
            $data['receipt_type']=3;
            $receipt_type_name='银记';
        }

        $count_res=self::getmoneylogcount($data['type'],$data['receipt_type']);

        $receipt_no=$receipt_type_name.$count_res['no'];

        $fee=self::getFeeByMoney($income_money,$pay_type_id);
        $data['issys']=2;
        $data['abstract']=$sn_code;
        $data['receipt_no']=$receipt_no;
        $feedata=$data;
        $data['inout_type']=2;
        $last_moneylog=FinancialMoneylog::where(['type'=>$data['type'],'is_del'=>1])->orderBy('id','desc')->first();
        $last_moneylog_residue_money=$last_moneylog?$last_moneylog->residue_money:0;
        $data['residue_money']=$last_moneylog_residue_money+$income_money;

        $feedata['inout_type']=1;
        if($feedata['type']==1){
            $feedata['receipt_type']=2;

            $count_res=self::getmoneylogcount($feedata['type'],$feedata['receipt_type']);

            $receipt_no=$receipt_type_name.$count_res['no'];


            $feedata['receipt_type']=2;
            $receipt_type_name='现付';
            $count_res=self::getmoneylogcount($feedata['type'],$feedata['receipt_type']);
            $receipt_no=$receipt_type_name.date('n').'-'.($count_res['count']).'#';

        }else{
            $receipt_no=$receipt_type_name.date('n').'-'.($count_res['count']+1).'#';
        }
        $feedata['receipt_no']=$receipt_no;
        $feedata['abstract']=$sn_code;
        $feedata['income_money']=0;
        $feedata['expenditure_money']=$fee;
        $feedata['residue_money']= $data['residue_money']-$fee;
        $feedata['subject_id']=-5;

        FinancialMoneylog::create($data);
        FinancialMoneylog::create($feedata);
    }
}
